import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

class Solution{

    //1.水果总值
    public static void main1(String[] args){
        Scanner in = new Scanner(System.in);

        long n = in.nextInt(),m = in.nextInt(),a = in.nextInt(),b = in.nextInt();

        //枚举所有情况
        //1.计算出 苹果的所有可能取值
        long ret = 0;
        for(long x = 0; x <= Math.min(n / 2,m);x++){

            //获取桃子的选择
            long y = Math.min(n - x * 2,(m - x) / 2);

            ret = Math.max(x * a + y * b,ret);
        }
        System.out.println(ret);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i= 0;i < n;i++)  arr[i] = in.nextInt();

        int ret = 0;
        for(int i = 1;i < n;i++){
            if(arr[i] > arr[i -1])
                ret += arr[i] - arr[i -1];
        }

        System.out.println(ret);

    }

    public  static void  reverse(char[] s,int left,int right){
        while(left < right){
            char tem = s[left];
            s[left] = s[right];
            s[right] = tem;
            left++;
            right--;
        }
    }
    public static void main3(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] s = br.readLine().toCharArray();

        int n = s.length;
        reverse(s,0,n -1);

        int left = 0;
        while(left < n){
            int right = left;
            while(right < n && s[right] != ' ') right++; //找单词
            reverse(s,left,right -1);
            while(right < n && s[right] == ' ') right++;//跳过空白字符
            left = right; //更新left
        }

        for(char x : s) System.out.print(x);

    }


}